package com.mp.chart.renderer;

import android.graphics.Canvas;

import com.mp.chart.animation.ChartAnimator;
import com.mp.chart.charts.KLineChart;
import com.mp.chart.interfaces.BarLineScatterCandleDataProvider;
import com.mp.chart.utils.Highlight;
import com.mp.chart.utils.ViewPortHandler;

import java.util.ArrayList;
import java.util.List;


public class AJChartRenderer extends DataRenderer {

    /**
     * all rederers for the different kinds of data this combined-renderer can
     * draw
     */
    protected List<DataRenderer> mRenderers;

    public AJChartRenderer(KLineChart chart, ChartAnimator animator,
                           ViewPortHandler viewPortHandler) {
        super(animator, viewPortHandler);

        createRenderers(chart, animator, viewPortHandler);
    }

    /**
     * Creates the renderers needed for this combined-renderer in the required
     * order. Also takes the DrawOrder into consideration.
     *
     * @param chart
     * @param animator
     * @param viewPortHandler
     */
    protected void createRenderers(KLineChart chart, ChartAnimator animator,
                                   ViewPortHandler viewPortHandler) {

        mRenderers = new ArrayList<>();

        KLineChart.DrawOrder[] orders = chart.getDrawOrder();

        for (KLineChart.DrawOrder order : orders) {

            switch (order) {
                case BAR:
                    if (chart.getBarData() != null)
                        mRenderers.add(new AJBarChartRenderer(chart, animator, viewPortHandler));
                    break;

                case BUBBLE:
                    if (chart.getBubbleData() != null)
                        mRenderers.add(new BubbleChartRenderer(chart, animator, viewPortHandler));
                    break;
                case LINE:
                    if (chart.getLineData() != null)
                        mRenderers.add(new AJLineChartRenderer(chart, animator, viewPortHandler));
                    break;
                case CANDLE:
                    if (chart.getCandleData() != null)
                        mRenderers.add(new AJKLineStickChartRenderer(chart, animator,
                                viewPortHandler));
                    break;
                case POINT:
                    if (chart.getScatterData() != null)
                        mRenderers.add(new AJPointChartRenderer(chart, animator, viewPortHandler));
                    break;
                case SAR:
                    if (chart.getSarData() != null)
                        mRenderers.add(new SarChartRenderer(chart, animator, viewPortHandler));
                    break;
                case BOLL:
                    if (chart.getBollData() != null)
                        mRenderers.add(new BollChartRenderer(chart, animator, viewPortHandler));
                    break;

            }
        }
    }


    @Override
    public void initBuffers() {

        for (DataRenderer renderer : mRenderers)
            renderer.initBuffers();
    }

    @Override
    public void drawData(Canvas c) {

        for (DataRenderer renderer : mRenderers)
            renderer.drawData(c);
    }

    @Override
    public void drawValues(Canvas c) {

        for (DataRenderer renderer : mRenderers)
            renderer.drawValues(c);
    }

    @Override
    public void drawExtras(Canvas c) {

        for (DataRenderer renderer : mRenderers)
            renderer.drawExtras(c);
    }

    @Override
    public void drawHighlighted(Canvas c, Highlight[] indices) {
        for (DataRenderer renderer : mRenderers)
            renderer.drawHighlighted(c, indices);
    }

    @Override
    public void calcXBounds(BarLineScatterCandleDataProvider chart, int xAxisModulus) {
        for (DataRenderer renderer : mRenderers)
            renderer.calcXBounds(chart, xAxisModulus);
    }

    /**
     * Returns the sub-renderer object at the specified index.
     *
     * @param index
     * @return
     */
    public DataRenderer getSubRenderer(int index) {
        if (index >= mRenderers.size() || index < 0)
            return null;
        else
            return mRenderers.get(index);
    }
}